package vehement;

import java.util.Random;

public class Mapmaker {
	
	private int height_max;
	private int width_max;
	private int room_max;
	private int[][] map;
	
	//Note: make sure fields are positive
	//Note: should check to make sure we can fit rooms in space
	public Mapmaker(int max_height, int max_width, int number_rooms)
	{
		height_max = max_height;
		width_max = max_width;
		room_max = number_rooms;
		Generate();
	}
	
	public Mapmaker(int number_rooms)
	{
		height_max = 16;
		width_max = 16;
		room_max = number_rooms;
		Generate();
	}
	
	//This is for a quick cross
	public Mapmaker()
	{
		height_max = 5;
		width_max = 5;
		room_max = 5;
		int start = 17;
		map = new int[width_max][height_max];
		for(int i = 0; i < width_max; i++)
		{
			for(int j = 0; j < height_max; j++)
			{
				map[i][j] = -1; 
			}
		}
		map[2][2] = 16 + 15;
	}
	
	//Basic code:
	/**
	 * 0 = no paths
	 * 1 = path north
	 * 2 = path south
	 * 4 = path west
	 * 8 = path east
	 * 1+4 = paths north and west
	 * and so on
	 * 
	 * All start out -1
	 * -2 means added to queue, but not built
	 * 16 is start
	 * 
	 * Need a queue for rooms yet to have path added
	 */
	
	private void Generate()
	{
		int start; //Index of the start in base height
		int new_room; //Index of the new room
		int paths; //Paths of the new room
		int total_rooms = 0;
		map = new int[width_max][height_max];
		Random generator = new Random();
		for(int i = 0; i < width_max; i++)
		{
			for(int j = 0; j < height_max; j++)
			{
				map[i][j] = -1; 
			}
		}
	    start = generator.nextInt(height_max * width_max);
	    map[start / width_max][start % width_max] = 16;
	    total_rooms++;
	    if(total_rooms == room_max)
	    {
	    	//done
	    }
	    paths = generator.nextInt(15) + 1;
	    //NOTE: check edges, surrounding rooms
	    if(paths != 0)
	    {
	    	if(paths%2==1)
	    	{
	    		if(start/width_max==0)
	    			paths -= 1;
	    		else
	    			;//add to queue if == -1
	    	}
	    	if(paths%4/2==1)
	    	{
	    		if(start/width_max==height_max-1)
	    	  		paths -= 2;
	    		else
	    			;//add to queue if == -1
	    	}
	    	if(paths%8/4==1)
	    	{
	    		if(start%width_max==0)
	    			paths -= 4;
	    		else
	    			;//add to queue if == -1
	    	}
	    	if(paths%16/8==1)
	    	{
	    		if(start%width_max==width_max-1)
	    			paths -= 8;
	    		else
	    			;//add to queue if == -1
	    	}
	    }
	    map[start / width_max][start % width_max] += paths;
	    
	}
	
	public int[][] Get_Grid()
	{
		return map;
	}
	public int get_width()
	{
		return width_max;
	}
	public int get_height()
	{
		return height_max;
	}
}
